package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RequestMapping("/employee")
@Slf4j
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,HttpSession session){
        R<Employee> result = employeeService.login(employee);

        if (result.getData() != null){
            session.setAttribute("employee",result.getData().getId());
        }
        return result;
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setCreateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        employeeService.save(employee);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        R<Page> result = employeeService.findByPage(page, pageSize, name);
        return result;
    }


    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable Long id){
        R<Employee> result = employeeService.findById(id);
        return result;
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        employeeService.update(employee);
        return R.success("修改成功");
    }



    @RequestMapping("/logout")
    public R<String> logout(HttpSession session){
        session.invalidate();
        return R.success("成功退出");
    }

//    @RequestMapping("/login")
//    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
//
//        R<Employee> result = employeeService.login(employee);
//
//        if (result.getData() != null){
//            session.setAttribute("employee",result.getData().getId());
//        }
//        return result;
//    }
//
//    @PostMapping
//    public R<String> save(@RequestBody Employee employee,HttpSession session){
//        Long empId = (Long) session.getAttribute("employee");
//
//        employee.setUpdateUser(empId);
//        employee.setCreateUser(empId);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        employeeService.save(employee);
//        return R.success("保存成功");
//    }
//
//    @GetMapping("/page")
//    public R<Page> page(int page,int pageSize,String name){
//        R<Page> result = employeeService.findByPage(page, pageSize, name);
//        return result;
//    }
//
//    @PutMapping
//    public R<String> update(@RequestBody Employee employee,HttpSession session){
//        Long empId = (Long) session.getAttribute("employee");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
//        employeeService.updateById(employee);
//        return R.success("成功修改员工信息");
//    }
//
//    @GetMapping("/{id}")
//    public R<Employee> findById(@PathVariable Long id){
//        R<Employee> result = employeeService.findById(id);
//        return result;
//    }
//
//    @RequestMapping("/logout")
//    public R<String> logout(HttpSession session){
//        session.invalidate();
//        return R.success("退出成功");
//    }



}
